package nsq

import (
	"fmt"
	"encoding/json"
	"meman/libs/utils"
)

type Nsq struct  {
	StatusCode int  `json:"status_code"`
	StatusTxt string  `json:"status_txt"`
	Data Data `json:"data"`

}

type Data struct  {
	Version string  `json:"version"`
	Health string   `json:"health"`
	StartTime int64 `json:"start_time"`
	Topics []Topic `json:"topics"`
}

type Topic struct {
	TopicName string `json:"topic_name"`
	Channels []Channel `json:"channels"`
	Depth int `json:"depth"`
	BackendDepth int  `json:"backend_depth"`
	MessageCount int `json:"message_count"`
	Paused bool `json:"paused"`
	//e2e_processing_latency  //暂时不实现并没有使用
}

type Channel struct {
	ChannelName string `json:"channel_name"`
	Depth int `json:"depth"`
	BackendDepth int  `json:"backend_depth"`
	InFlightCount int `json:"in_flight_count"`
	DeferredCount int `json:"deferred_count"`
	MessageCount int `json:"message_count"`
	RequeueCount int `json:"requeue_count"`
	TimeoutCount int `json:"timeout_count"`
	Clients []Client `json:"clients"`
	Paused bool `json:"paused"`
	//e2e_processing_latency  //暂时不实现并没有使用

}

type Client struct  {
	Name string `json:"name"`
	ClientId string `json:"client_id"`
	Hostname string `json:"hostname"`
	Version string  `json:"version"`
	RemoteAddress string `json:"remote_address"`
	State int `json:"state"`
	ReadyCount int `json:"ready_count"`
	InFlightCount int `json:"in_flight_count"`
	MessageCount int `json:"message_count"`
	FinishCount int `json:"finish_count"`
	RequeueCount int `json:"requeue_count"`
	ConnectTs int64  `json:"connect_ts"`
	SampleRate int  `json:"sample_rate"`
	Deflate bool `json:"deflate"`
	Snappy bool `json:"snappy"`
	UserAgent string `json:"user_agent"`
	Tls bool `json:"false"`
	//tls_cipher_suite string
	//tls_version string
	//tls_negotiated_protocol string
	//tls_negotiated_protocol_is_mutual bool
}



func GetNsq(host string) (*Nsq, error) {
	url := fmt.Sprintf("http://%s:4151/stats?format=json", host)
	responses, err := utils.RemoteContent(url)
	if err != nil {
		return nil, fmt.Errorf("Auth: get http content err:%s", err.Error())
	}
	var nsq *Nsq
	err = json.Unmarshal(responses, &nsq)
	if err != nil {
		return nil, fmt.Errorf("Auth: json unmarshal user info error!err:%s", err.Error())
	}

	return nsq, nil
}